package com.dyx.lifecycle;

import java.util.ArrayList;
import java.util.List;

/**
 * @author dengyouxu
 * @description TODO
 * @since 2021-05-10 下午4:33
 */
public class Server implements LifeCycle {

    private List<LifeCycleListener> listeners = null;

    private LifeCycleEvent lifeCycleEvent = null;

    public Server() {
        if (listeners == null) {
            listeners = new ArrayList<>();
        }
        listeners.add(new LogLifeCycleListener());

    }

    public List<LifeCycleListener> getListeners() {
        return listeners;
    }

    public boolean addListener(LifeCycleListener listener) {
        if (listener == null) return false;
        return listeners.add(listener);

    }

    public boolean removeListener(LifeCycleListener listener) {
        if (listener == null) return false;
        return listeners.remove(listener);
    }

    public void start() {
        lifeCycleEvent = LifeCycleEvent.START;
        getListeners().forEach((LifeCycleListener listener) -> listener.doSomeThingOnEvent(lifeCycleEvent));
    }

    public void stop() {
        lifeCycleEvent = LifeCycleEvent.STOP;
        getListeners().forEach((LifeCycleListener listener) -> listener.doSomeThingOnEvent(lifeCycleEvent));
    }
}
